import cookie from 'react-cookies';
import { TOKEN } from '@/constants/tokenConstant.js';

// 有效期(天)
export const TOKEN_EXPIRED_DAY = 1;

const getToken = () => {
  return cookie.load(TOKEN);
};

function setToken(token) {
  let inFifteenMinutes = new Date(new Date().getTime() + 24 * TOKEN_EXPIRED_DAY * 3600 * 1000);
  cookie.save(TOKEN, token, { path: '/', expires: inFifteenMinutes });
}

function removeToken() {
  cookie.remove(TOKEN, { path: '/' });
}

const TokenUtil = {
  getToken,
  setToken,
  removeToken
};

export default TokenUtil;
